<template>
  <div class="home">
    <!--  头部 -->
    <van-nav-bar fixed  placeholder title="cgv影城" />

    <!-- banner -->
    <!-- <van-button type="primary">主要按钮</van-button> -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in banner" :key="item._id">
        <img :src="item.img" alt="" />
      </van-swipe-item>
    </van-swipe>

    <!--  定位 -->

    <div class="homeTabBar">
      <div class="location" @click="$router.push('/city')">
        <svg class="xl-icon" aria-hidden="true">
          <use xlink:href="#icon-31dingwei"></use>
        </svg>
        <span>{{ shopName }}</span>
        <!-- 其实需要的是 电影院的名字   -->
        <svg class="xl-icon downarrow" aria-hidden="true">
          <use xlink:href="#icon-sanjiao"></use>
        </svg>
      </div>
      <!--  右侧 -->
      <div class="switchTab">
        <!-- tabActive 值是1  叫给这个div加上act的class  -->
        <div :class="{act:tabActive==1}"  @click="changeTab(1)">在售</div>
        <div :class="{act:tabActive==2}" @click="changeTab(2)">即映</div>
      </div>
    </div>
    <!--  电影列表 -->

    <div class="films-list"  v-if="status == 'loaded'">
      <div class="films-item" v-for="item in filmsList" :key="item._id" @click="$router.push(`/filmsIntro?filmsId=${item._id}`)">
        <div class="films-item-img">
           <img :src="item.img" alt="">
        </div>
        <div class="films-item-content">
          <div class="title">
               {{item.name}}
          </div>
          <div class="formats">
            <div class="label" v-for="(item2,index) in item.formats" :key="index">  
              {{item2.split('-')[0]}} <span> {{item2.split('-')[1]}}</span>
            </div>
          </div>
                <div class="score">
                    {{item.score}}分
            </div>
            <div class="desc">
                 {{item.stage}}
            </div>

        </div>
        <div class="films-item-btn">
           <van-button v-if="item.status == 1" round color="#DB281F">购票</van-button>
           <div class="like" v-if="item.status == 2">
            <van-icon name="like-o" />
            <p>想看</p>
           </div>
        </div>

      </div>
    </div>

     <!--   loading -->
      <div v-if="status == 'loading'" class="text-center" style="margin-top:50px">
        <van-loading type="spinner" />
      </div>
      <!--   empty -->
        <van-empty v-if="status == 'empty'" description="暂无数据" />

      <!-- the end -->
  </div>
</template>

<script>
// @ is an alias to /src

import { Swipe, SwipeItem } from "vant";
export default {
  //  尽量加name  
  name: "HomeView",
  components: {
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem,
  },
  data() {
    return {
      banner: [],
      shopName: "",
      filmsList:[],//  
      status:'loading',
      tabActive:1
    };
  },
  created() {
    this.getBanner();
  },
  //  写法 必须得来一下首页  
  // 激活  加了keep-alive后才有
  async activated() {
    let shopName = localStorage.getItem("shopName");
    let shopId = localStorage.getItem("shopId");

    if (!shopId) {
      // 第一次进来  本地存储没
      // 1.  提示一下用户 让他去选
      //
      //  获取到定位
      //  获取到定位城市的影院
      //  推荐一个给用户

      let cityName = await this.LocalCity(); //  拿到定位 城市名
      // cityName = '洛阳市'
      console.log(cityName);
      


      let shopList = await this.getShopList(cityName); //  根据城市名 拿影院
      console.log(shopList)
      let shopInfo = shopList.rows[0]; //  默认推荐第一个

      // 如果没数据  
      if(!shopInfo){
        
      this.$dialog
        .alert({
          title: "温馨提示",
          message: `${cityName}暂无门店,请您手动选择.`,
        })
        .then((res) => {
       
               console.log(res);
              setTimeout(v=>{
                 this.$router.push('/city')
              },1000)
        })

       return 


      }

      this.$dialog
        .confirm({
          title: "温馨提示",
          message: `当前定位为${cityName},是否切换到【${shopInfo.name}】影城?`,
        })
        .then((res) => {
          //  去选
          let { name, _id } = shopInfo;
          localStorage.setItem("shopName", name);
          localStorage.setItem("shopId", _id);
          localStorage.setItem("shopInfo", JSON.stringify(shopInfo));
          this.shopName = name;
          this.shopId = _id;

          //  去获取当前这个影院的 上映中的电影
          this.getFilmsList();
        })
        .catch((err) => {
          //  点了去取消  跳去选
          this.$router.push("/city");
        });
    } else {
      //  如果原来有了
      console.log(shopName);
      this.shopName = shopName;
      this.shopId = shopId;
      //  去获取当前这个影院的 上映中的电影
      this.getFilmsList();
    }
  },
  methods: {
    getBanner() {
      this.$http.post("/apis/banner/pub/getList").then((res) => {
        console.log(res);
        this.banner = res.rows;
      });
    },
    // 获取位置
    LocalCity() {
      return new Promise((resolve, reject) => {
        let myCity = new BMapGL.LocalCity();
        myCity.get((res) => {
          resolve(res.name);
        });
      });
    },
    //  获取店铺
    getShopList(cityName) {
      return this.$http.post("/apis/shop/pub/getList", {
        city: cityName,
      });
    },
    //  获取电影列表
    getFilmsList() {
      let { shopId ,tabActive} = this;
      this.$http
        .post("/apis/films/pub/getList", {
          shopId,
          status:tabActive
        })
        .then((res) => {
          console.log(res);
          this.$toast.clear()
          if(res.code==0){
            this.filmsList = res.rows;
            // if(this.filmsList.length){
            //   this.status = 'loaded'
            // }else{

            //   this.status = 'empty'
            // }
            
            this.status = this.filmsList.length?'loaded':'empty'

          }
        });
    },
    //  切换 tab
    changeTab(num){
  //  num  是1    
     this.tabActive = num;
      this.$toast.loading({
        message:'加载中'
      });
        
        //   没清数据 
        this.filmsList.length = 0
      this.getFilmsList()

    }
  },
};
</script>

<style lang="scss">
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  img {
    width: 100%;
    height: 112px;
  }
}

.homeTabBar {
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  .location {
    font-size: 18px;
  }
  .xl-icon {
    font-size: 22px;
  }
  .downarrow {
    color: #999;
  }

  .switchTab {
    height: 40px;
    display: flex;
    align-items: center;
    div {
      width: 50px;
      font-size: 16px;
      font-weight: 700;
      text-align: center;
      height: 40px;
      line-height: 40px;
       border-bottom: 2px solid transparent;
      &.act {
        color: $baseColor;
        border-color: $baseColor;
      }
    }
  }
}


//  films-list
.films-list{
  padding-left:10px;
  padding-top: 10px;

}

.films-item{
  display: flex;
  // justify-content: space-between;
  border-bottom: 1px solid #eee;
  padding: 5px 5px 5px 0;
  &-img{
    width: 68px;
    height: 95px;
    img{
        width: 68px;
      height: 95px;
    }
  }
  &-content{
    padding:0 8px;
    width: 200px;
    .score,.desc{
      color: #999;
      font-size: 12px;
      margin-top: 3px;
    }
    .desc{
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .title{
      font-size: 16px;
      font-weight: bold;
      line-height: 24px;
    }
    .formats{
      overflow: hidden;
        .label{
          display: block;
          background: rgba(147,145,170,1);
          border-radius: 2px;
          padding: 1px 2px;
          float: left;
          margin: 1px;
          color: #fff;
          font-size: 9px;
          transform: scale(0.9);
          span{
             color:rgba(147,145,170,1);
             background: #fff;
             padding: 0 1px;
             border-radius: 2px;
          }
        }
    }
  }
  &-btn{
    flex:1;
    display: flex;
    justify-content: center;
    align-items: center;

  }
  .like{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
  }
}

</style>
